//
//  ZHLEETabBar.swift
//  ZHLEEBlog_swift_pro
//
//  Created by ZHLEEBlander on 15/10/11.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEETabBar: UITabBar {
    
    var plusButtonBlock: (()->())?
    
    var plusButton :UIButton?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        let plusButton = UIButton()
        plusButton.addTarget(self, action: "plusButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        let imageName = "tabbar_compose_icon_add"
        let imageBGName = "tabbar_compose_button"
        
        plusButton.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        plusButton.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Selected)
        
        plusButton.setBackgroundImage(UIImage(named: imageBGName), forState: UIControlState.Normal)
        plusButton.setBackgroundImage(UIImage(named: "\(imageBGName)_highlighted"), forState: UIControlState.Selected)
        plusButton.sizeToFit()
        
        addSubview(plusButton)
        
        self.plusButton = plusButton
    }
    
    @objc private func plusButtonDidClick(){
        print("发布按钮点击")
        
        if let block = self.plusButtonBlock{
        
            block()
        }
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.plusButton?.center = CGPointMake(self.centerX, self.height * 0.5)
        
        let childW = self.width / 5
        var index = 0
        for childView in self.subviews {

            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
//                            print(childView)
                childView.x = CGFloat(index) * childW
                
                index++
                
                if index == 2{
                
                    index++
                }
            }
        }
    }
}
